<template>
  <div class="sender">
    <div class="sender-content">
      <Sender
        v-model="senderValue"
        submitType="enter"
        @submit="handleSubmit"
        :loading="senderLoading"
        :auto-size="{
          minRows: 3,
          maxRows: 6,
        }"
        :disabled="chatItemStore?.state?.loading"
        variant="updown"
      >
        <template #prefix>
          <div
            class="query-device"
            :class="{ 'query-device--active': chatStore.state.sendbox.isQueryDevice }"
            @click="handleClickQueryDevice"
          >
            <el-icon><Tools /></el-icon>
            <span>查询设备</span>
          </div>
        </template>
        <!-- 自定义操作列表 -->
        <template #action-list>
          <div class="action-list-self-wrap">
            <el-button v-if="senderLoading" type="primary" plain circle @click="handleCancel">
              <el-icon class="is-loaidng">
                <Loading />
              </el-icon>
            </el-button>

            <el-button v-else plain circle @click="handleSubmit">
              <el-icon><Position /></el-icon>
            </el-button>
          </div>
        </template>
      </Sender>

      <div class="prompt">
        <div
          class="prompt-item"
          v-for="item in tips"
          :key="item.value"
          @click="handleClick(item)"
          :class="{ 'prompt-item--disabled': chatItemStore?.state?.loading }"
        >
          <span>{{ item.label }}</span>
        </div>
      </div>

      <!-- <textarea v-model="keyword" @keyup.enter="handleEnter" placeholder="输入开始聊天" />
      <el-button @click="handleEnter" :disabled="state.loading" type="primary">发送</el-button> -->
    </div>
  </div>
</template>
<script setup>
import { Sender } from 'vue-element-plus-x'
import { ElMessage } from 'element-plus'
import { prompts } from '../dict.js'

import Input from './Input.vue'

const tips = Object.entries(prompts).map(([value, label]) => ({ value, label }))
const { chatStore } = inject('/chat')
const { state, scrollContainer, messages, chatItemStore } = chatStore

const senderValue = ref('')
const senderLoading = ref(false)
const handleSubmit = () => {
  senderLoading.value = true
  setTimeout(() => {
    try {
      chatStore.handleInput(senderValue.value)
    } catch (e) {
      console.error(e)
    }

    senderLoading.value = false
    senderValue.value = ''
  }, 500)
}

const handleEnter = () => {
  try {
    chat.input(keyword.value)
  } catch (e) {
    console.error(e)
  }
  keyword.value = ''
}

const handleClick = item => {
  if (chatStore.state.loading) return
  chatStore.handleInput(item.value)
}

const handleClickQueryDevice = () => {
  chatStore.state.sendbox.isQueryDevice = !chatStore.state.sendbox.isQueryDevice
}
</script>
<style lang="scss" scoped>
.sender {
  position: sticky;
  left: 0;
  bottom: 0;
  z-index: 1;
  padding-top: 16px;
}

// .sender-content {
//   background-color: #f3f4f6;
//   width: 800px;
//   margin: 0 auto;
//   border: 1px solid #ddd;
//   border-radius: 6px;
//   overflow: hidden;
//   padding: 12px;

//   textarea {
//     border: none;
//     outline: none;
//     width: 100%;
//     height: 100px;
//     background: none;
//     font-family: inherit;
//     font-size: 16px;
//   }
// }

.sender-content {
  .el-sender-wrap {
    background-color: #fff;
  }
}

.prompt {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin-top: 16px;
  .prompt-item {
    background-color: #fff;
    border: 1px solid #ddd;
    padding: 6px 12px;
    border-radius: 24px;
    font-size: 12px;
    cursor: pointer;
    &:hover {
      background-color: #f5f5f5;
    }
  }

  .prompt-item--disabled {
    cursor: not-allowed;
    color: rgba($color: #000, $alpha: 0.5);
    &:hover {
      background: none;
    }
  }
}

.query-device {
  border: 1px solid #ddd;
  padding: 4px 12px;
  border-radius: 12px;
  cursor: pointer;
  border: 1px solid #c5c8d0;
  font-size: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  &:hover {
    background-color: #e0e4ed;
  }
  .el-icon {
    margin-right: 4px;
    font-size: 14px;
  }
}

.query-device--active {
  background-color: #eff6ff;
  border-color: #bfdbfe;
  color: #2563eb;
}
</style>
